﻿using System;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Reactive;
using System.Reactive.Linq;
using ManagerClient.AuthentificationService;
using Microsoft.Practices.Prism.Commands;

namespace ManagerClient.Manage.Logic
{
    public class UserWindowViewModel : ViewModelBase
    {
        private readonly AuthentificationServiceClient client;
        private string login;
        private string name;
        private string password;
        private UserRoles role;
        private string surname;


        public UserWindowViewModel(AuthentificationServiceClient client)
        {
            SaveUserCommand = new DelegateCommand(SaveUser);
            this.client = client;
        }


        public DelegateCommand SaveUserCommand { get; set; }

        [DisplayName("Name")]
        [Required]
        public string Name
        {
            get { return name; }
            set
            {
                name = value;
                ValidateProperty("Name");
                RaisePropertyChanged(() => Name);
            }
        }

        [DisplayName("Surname")]
        [Required(ErrorMessage = "Field 'Range' is required.")]
        public string Surname
        {
            get { return surname; }
            set
            {
                surname = value;
                RaisePropertyChanged(() => Surname);
            }
        }

        [DisplayName("Login")]
        [Required]
        public string Login
        {
            get { return login; }
            set
            {
                login = value;
                RaisePropertyChanged(() => Login);
            }
        }

        [Required]
        public string Password
        {
            get { return password; }
            set
            {
                password = value;
                RaisePropertyChanged(() => Password);
            }
        }

        [Required]
        public UserRoles Role
        {
            get { return role; }
            set
            {
                role = value;
                RaisePropertyChanged(() => Role);
            }
        }

        private void SaveUser()
        {
            var user = new User
                {
                    DepartmentId = 1,
                    Login = Login,
                    Password = Password,
                    Name = Name,
                    Surname = Surname,
                    AccountState = 15,
                    UserRole = UserRole.Orders
                };
            IsBusy = true;
            IObservable<Unit> func = Observable.FromAsyncPattern<User>(
                client.BeginAddUser, client.EndAddUser)(user);
            func.
                ObserveOnDispatcher().
                Finally(() => { IsBusy = false; }
                )
                .Subscribe(x => { },
                           e => { }
                );
        }
    }
}